package com.cskaoyan.service.impl;


import com.cskaoyan.config.aliyunComponent.AliyunApplication;
import com.cskaoyan.mapper.MessageMapper;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.UserService;
import com.cskaoyan.model.User;

import com.cskaoyan.model.UserExample;

import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author huzhouming
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;

    @Autowired
    AliyunApplication aliyunApplication;
    @Autowired
    HttpServletRequest request;

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description:处理获取用户管理模块会员管理信息的操作
     * @date: 2022/5/5
     * @param param
     * @param username
     * @return com.cskaoyan.model.vo.UserDataVO
     */
    public DataVO query(BaseParamBO param, String username, String mobile) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        if (username != null) {
            if (username == "") {
                username = null;
            }
        }
        if (mobile != null) {
            if (mobile == "") {
                mobile = null;
            }
        }
        List<User> list = userMapper.select1(sort, order, username, mobile);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<User> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description: 修改用户信息
     * @date: 2022/5/5
     * @param user
     * @return void
     */
    public void updateUser(User user) {
        user.setId(null);
        if (user.getWeixinOpenid() == null) {
            user.setLastLoginIp("");
        }
        if (user.getLastLoginIp() == null) {
            user.setWeixinOpenid("");
        }
        if (user.getSessionKey() == null) {
            user.setSessionKey("");
        }
        if (user.getWeixinOpenid() == null) {
            user.setWeixinOpenid("");
        }
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        userMapper.updateByExampleSelective(user, userExample);
    }

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description: 按照用户id查找用户信息
     * @date: 2022/5/5
     * @param id
     * @return com.cskaoyan.model.User
     */
    public User selectUserById(int id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    /**
     * @author: lixiaosheng
     * @description: 获取用户的名称和头像
     * @date: 2022/5/8 21:25
     */
    public WxLoginDataVO<UserLoginVO> getAdminInfoByName(String username) {
        UserLoginVO userLoginVO = userMapper.selectUserLoginInfoByUsername(username);
        WxLoginDataVO<UserLoginVO> login = new WxLoginDataVO<>();
        login.setUserInfo(userLoginVO);
        return login;
    }

    /**
     * @author: zk
     * @description: TODO 根据用户名获取用户id
     * @date: 2022/5/8 22:42
     */
    @Override
    public Integer getUserIdByUsername(String username) {
        Integer id = userMapper.selectUserId(username);
        return id;
    }

    /**
     * @author: wlyq
     * @description: TODO 用户注册
     * @date: 2022/5/10 19:56
     */
    @Override
    public ExceptionVO userRegister(WxRegisterVO wxRegisterVO) {
        String mobile = wxRegisterVO.getMobile();
        String password = wxRegisterVO.getPassword();
        String username = wxRegisterVO.getUsername();
        String wxCode = wxRegisterVO.getWxCode();
        UserExample userExample1 = new UserExample();
        UserExample.Criteria criteria1 = userExample1.createCriteria();
        criteria1.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample1);
        if (users.size() != 0) {
            BaseRespVO baseRespVO = new BaseRespVO();
            baseRespVO.setErrno(703);
            baseRespVO.setErrmsg("用户名重复");
            return baseRespVO;
        }
        User user = new User();
        user.setAddTime(new Date());
        user.setWeixinOpenid(wxCode);
        user.setDeleted(false);
        user.setGender((byte) 0);
        user.setMobile(mobile);
        user.setPassword(password);
        user.setUsername(username);
        user.setUserLevel((byte) 0);
        user.setUpdateTime(new Date());
        user.setNickname(username);
        user.setStatus((byte) 0);
        user.setLastLoginTime(new Date());
        user.setLastLoginIp(request.getRemoteAddr());
        user.setAvatar("http://localhost:8083/image/14651728-4fc5-4751-9854-b25b5b40b4a7-Yasuo0.jpg");
        userMapper.insertSelective(user);
        return ExceptionVO.fine();
    }

    @Override
    public void sendMsg(String mobile, String code) {
        aliyunApplication.sendMsg(mobile, code);
        messageMapper.update(mobile, code);
    }

    @Override
    public boolean checkCode(String mobile, String code) {
        Integer id = messageMapper.selectByMobileAndCode(mobile, code);
        return id != null;
    }

    @Override
    public ExceptionVO reset(String phone, String password, String code) {
        if (!checkCode(phone, code)) {
            return ExceptionVO.error(711, "验证码错误");
        }
        User user = new User();
        user.setPassword(password);
        user.setUpdateTime(new Date());
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andMobileEqualTo(phone);
        criteria.andDeletedEqualTo(false);
        Integer row = userMapper.updateByExampleSelective(user, userExample);
        if (row == 0) {
            return ExceptionVO.error(710, "手机号码格式错误");
        }
        return ExceptionVO.fine();
    }
}
